package com.kcsj.service;
//李延杰
import com.kcsj.dao.WarehouseDao;
import com.kcsj.domain.ProductModel;
import com.kcsj.domain.Warehouse;
import com.kcsj.util.Condition;
import com.kcsj.util.JdbcHelper;
import com.kcsj.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class WarehouseService {
    public static final WarehouseService warehouseService = new WarehouseService();
    public static final WarehouseDao warehouseDao = WarehouseDao.getInstance();

    public static WarehouseService getInstance(){
        return warehouseService;
    }

    public Collection<Warehouse> findAll()throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<Warehouse> deliveryPeoples = warehouseDao.findAll(connection);
        JdbcHelper.close(connection);
        return deliveryPeoples;
    }

    public Warehouse find(String warehouseNo)throws SQLException{
        Connection connection = JdbcHelper.getConn();
        Warehouse productModel = warehouseDao.find(warehouseNo,connection);
        JdbcHelper.close(connection);
        return productModel;
    }

    public boolean update(Warehouse warehouseNo) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean judge = false;
        try {
            judge = warehouseDao.update(warehouseNo,connection);
            connection.commit();
        }catch (SQLException e) {
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return judge;
    }

    public int add(Warehouse warehouseNo) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        int num = 0;
        try{
            num = warehouseDao.add(warehouseNo,connection);
            connection.commit();
        }catch (SQLException e) {
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return num;
    }

    public boolean delete(String warehouseNo) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean judge = false;
        try{
            judge = warehouseDao.delete(warehouseNo,connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }

        return judge;
    }

}
